package com.dotflat.staffExtend.service.impl;


import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.staffExtend.domain.StaffExtendCareer;
import com.dotflat.staffExtend.domain.StaffExtendInfoRank;
import com.dotflat.staffExtend.domain.StaffExtendInfoRankCircle;
import com.dotflat.staffExtend.dto.StaffExtendStatisticsDTO;
import com.dotflat.staffExtend.repository.StaffExtendCareerRepository;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankCircleRepository;
import com.dotflat.staffExtend.repository.StaffExtendInfoRankRepository;
import com.dotflat.staffExtend.service.StaffExtendCareerService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.service.remoteCall.DTO.StaffInfoDTO;
import com.matt.service.remoteCall.SsoCallService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class StaffExtendCareerServiceImpl extends AreableObjectBaseServiceImpl<StaffExtendCareer, StaffExtendCareerRepository> implements StaffExtendCareerService {



    @Autowired
    private StaffExtendInfoRankCircleRepository circleRepository;

    @Autowired
    private StaffExtendInfoRankRepository staffExtendInfoRankRepository;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    @Override
    public void setBaseRepository(StaffExtendCareerRepository repository){
        super.setBaseRepository(repository);
    }

    @Override
    public void deleteAllByExtendInfoId(String extendInfoId){
        baseRepository.deleteAllByExtendInfoId(extendInfoId);
    }


    @Override
    public List<StaffExtendStatisticsDTO> ranking(String ticket) {
        StaffDTO staffDTO  = ssoCallService.getStaffDTO(ticket);
        String areaId = staffDTO.getAreaId();
        List<StaffExtendStatisticsDTO> extendStatisticsDTOList = new ArrayList<>();
        StaffExtendInfoRankCircle staffExtendInfoRankCircle = circleRepository.findFirstByAreaId(areaId);
        if (Objects.nonNull(staffExtendInfoRankCircle)){
            List<StaffExtendInfoRank>  list = staffExtendInfoRankRepository.findFirstByAreaIdAndRankCircleId(areaId,staffExtendInfoRankCircle.getId(),6);
            List<String> staffIds = list.stream().map(k->k.getStaffId()).collect(Collectors.toList());
            List<StaffInfoDTO>  staffInfoList =  ssoCallService.getStaffInfoList(ticket,staffIds);

            for (int i = 0; i < list.size(); i++) {
                StaffExtendStatisticsDTO statisticsDTO = new StaffExtendStatisticsDTO();
                for (int j = 0; j < staffInfoList.size() ; j++) {
                    if (StringUtils.equals(list.get(i).getStaffId(),staffInfoList.get(j).getId())){
                        statisticsDTO.setStaffId(staffInfoList.get(j).getId());
                        statisticsDTO.setGender(staffInfoList.get(j).getGender());
                        statisticsDTO.setHeadImgUrl(staffInfoList.get(j).getHeadImgUrl());
                        statisticsDTO.setRealName(staffInfoList.get(j).getRealName());
                        statisticsDTO.setMobile(staffInfoList.get(j).getMobile());
                        statisticsDTO.setRoles(staffInfoList.get(j).getRoleNames());
                    }
                }
                statisticsDTO.setRankVal(list.get(i).getRankVal());
                statisticsDTO.setStudyTimeTotal(list.get(i).getStudyTimeTotal());
                extendStatisticsDTOList.add(statisticsDTO);
            }
        }
        return extendStatisticsDTOList;
    }
}
